package com.test28_类库应用;

import org.junit.Test;

class IPHostValidator implements IValidator {
    public static final String DEFAULT_EMAIL_REGEX = "\\d{1,3}\\.\\d{1,3}\\.\\d{1,3}\\.\\d{1,3}";    // 默认验证
    private String regex;   // 正则表达式
    private String content; // 要处理的内容

    public IPHostValidator(String content) {
        this(content, DEFAULT_EMAIL_REGEX);
    }

    public IPHostValidator(String content, String regex) {
        this.content = content;
        this.regex = regex;
    }

    @Override
    public boolean test() {
        if (this.content == null || "".equals(this.content)) {
            return false;
        }
        if ("0.0.0.0".equals(this.content) || "255.255.255.255".equals(this.content)) {
            return false;
        }
        return this.content.matches(this.regex);
    }
}

class ValidatorFactory2 {
    private ValidatorFactory2() {
    }

    public static IValidator getInstance(String content, String... regex) {
        if (regex.length == 0) {
            return new IPHostValidator(content);
        } else {
            return new IPHostValidator(content, regex[0]);
        }
    }
}

/**
 * IP地址验证
 */
public class YootkDemo_04 {

    @Test
    public void test1() {
        IValidator validator = ValidatorFactory2.getInstance("192.168.1.2");
        System.out.println(validator.test() ? "IP地址结构正确！" : "IP地址结构错误！");
    }
}
